/*
 * RoboEye - Web client for www.newocr.com that allows you
 * to recognize an image captured by the camera.
 *
 * Copyright (C) Oxygen Development, 2011
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.devoxy.roboeye.core;

import android.content.Context;
import com.devoxy.roboeye.R;
import com.devoxy.roboeye.global.ContextHolder;
import com.devoxy.roboeye.global.PreferencesHelper;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Oxygen Development.
 * Author: Dmitriy Tarasov (dm.vl.tarasov@gmail.com)
 * Date: 03.06.11
 * Time: 21:50
 */
public class FileHelper {

    private static final int BUFFER_SIZE = 4096;
    private static final String TXT_EXTENSION = ".txt";
    private static final String JPG_EXTENSION = ".jpg";

    public static void copyStreamContentAndCloseInput(InputStream is, OutputStream os) throws IOException {
        try {
            byte[] buffer = new byte[BUFFER_SIZE];
            int length;
            while ((length = is.read(buffer)) != -1) {
                os.write(buffer, 0, length);
            }
            os.flush();
        } finally {
            is.close();
        }
    }

    public static void saveToFile(String text, String fileName) throws IOException {
        PrintWriter pw = new PrintWriter(new FileWriter(fileName));
        pw.write(text);
        pw.close();
    }

    public static String getPhotoFileName() {
        DateFormat format = new SimpleDateFormat("dd_mm_yyyy_HH_MM");
        StringBuilder sb = new StringBuilder(PreferencesHelper.pathToPhotos());
        sb.append("/").append(format.format(new Date())).append(JPG_EXTENSION);
        return sb.toString();
    }

    public static String getTextFilePath(String fileName) {
        StringBuilder sb = new StringBuilder(PreferencesHelper.pathToResults());
        sb.append("/").append(fileName);
        if (!fileName.toLowerCase().endsWith(TXT_EXTENSION)) {
            sb.append(TXT_EXTENSION);
        }
        return sb.toString();
    }

    public static String getFileNameFromPath(String path) {
        return new File(path).getName();
    }

    public static boolean isImage(File file) {
        return file.getName().endsWith(JPG_EXTENSION);
    }

    public static String getTxtFileName(String photoFileName) {
        // TODO in the future get separate paths for photos and results
        Context context = ContextHolder.getInstance().getContext();
        return photoFileName.replace(JPG_EXTENSION, TXT_EXTENSION).replace(context.getString(R.string.photos_dir), context.getString(R.string.results_dir));
    }

}
